package com.digitalchina.trade.controller;

import com.digitalchina.trade.entity.TbAccount;
import com.digitalchina.trade.entity.TbBankCard;
import com.digitalchina.trade.mapper.AccountMapper;
import com.digitalchina.trade.service.BankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BankCardControler {
    @Autowired
    BankCardService service;
    @Autowired
    AccountMapper mapper;
    @RequestMapping(value = "/sendEmail",method = {RequestMethod.POST})
    public String sendMail(Model model, TbBankCard bankCard,HttpSession session) {
        //测试用
        TbAccount account=new TbAccount();
        account.setAccount_name("罗成文");
        TbAccount user=mapper.login(account);
        session.setAttribute("account",user);
        //判空
        if ("".equals(bankCard.getBandcard_num())){
            model.addAttribute("msg","银行卡号不能为空");
            return "sendEmail";
        }else {
            String key= service.sendMail(bankCard.getBandcard_num());
            session.setAttribute("bandcard_num",bankCard.getBandcard_num());
            session.setAttribute("key",key);
//            model.addAttribute("msg","请注意银行卡邮箱");
//            model.addAttribute("bandcard_num",bankCard.getBandcard_num());
//            model.addAttribute("bankcard_IDcard",bankCard.getBankcard_IDcard());
//            model.addAttribute("bankcard_name",bankCard.getBankcard_name());
            return "checking";
        }
    }
    @RequestMapping(value = "/check",method = {RequestMethod.POST})
    public String checking(Model model,HttpSession session,TbBankCard bankCard,String check){
        //判空
        if ("".equals(session.getAttribute("bandcard_num"))||
                "".equals(model.getAttribute("bankcard_IDcard"))||
                "".equals(model.getAttribute("bankcard_name"))||
                "".equals(session.getAttribute("key"))||
                "".equals(check)||
                "".equals(session.getAttribute("account"))
        ) return "checking";
        //验证码
        String key=(String)session.getAttribute("key");
        TbAccount account=(TbAccount) session.getAttribute("account");
        if (key.equals(check)){
            bankCard.setBandcard_num((String)session.getAttribute("bandcard_num"));
            if (service.bindingCard(account.getAccount_id(),bankCard)){
                model.addAttribute("msg","绑定成功");
                List<TbBankCard> list=  service.findCards(account.getAccount_id());
                model.addAttribute("list",list);
                return "bankCards";
            }else {
                model.addAttribute("msg","信息有误/银行卡已绑定");
                return "checking";
            }
        }else {
            model.addAttribute("msg","验证码有误");
            return "checking";
        }
    }
    @RequestMapping(value = "/bankCardList")
    public String findBankCards(Model model,HttpSession session){
        TbAccount account =(TbAccount) session.getAttribute("account");
        List<TbBankCard> list= service.findCards(account.getAccount_id());
        model.addAttribute("list",list);
        return "bankCards";
    }

}
